//
//  LLPScrollItem.m
//  LLP
//
//  Created by Porcellio on 10/16/12.
//  Copyright (c) 2012 Porcellio. All rights reserved.
//

#import "LLPScrollItemView.h"
#import "LLPScrollView.h"
#import "LLPScrollItem.h"

@implementation LLPScrollItemView

- (id)initWithItem:(LLPScrollItem *)item {

    self = [super init];
    if(self) {
    
        self.backgroundColor = [UIColor clearColor];
        _item = [item retain];
        
        [self addTarget:item.target action:item.selector forControlEvents:UIControlEventTouchUpInside];
        
        _deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _deleteButton.frame = CGRectMake(0.0, 0.0, 20.0, 20.0);
        _deleteButton.center = CGPointMake(50-10, +10);
        _deleteButton.backgroundColor = [UIColor blackColor];
        [_deleteButton addTarget:self action:@selector(onDelete) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_deleteButton];
        
        _isEditing = NO;
        _deleteButton.hidden = YES;
    }
    
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    if(_item.image != nil) {
    
        [_item.image drawInRect:self.bounds];
    }
}


- (void)dealloc {

    if(_item != nil) {
        [self removeTarget:_item.target action:_item.selector forControlEvents:UIControlEventTouchUpInside];
    }
    
    [_item release];
    _item = nil;
    _deleteButton = nil;
    [super dealloc];
}

#pragma mark - 

- (void)setIsEditing:(BOOL)isEditing{
    _isEditing = isEditing;
    
    if (isEditing) {
        _deleteButton.hidden = NO;
    }else{
        _deleteButton.hidden = YES;
    }
}

- (void)onDelete{
    [UIView animateWithDuration:0.3 animations:^{
        _deleteButton.hidden = YES;
        self.alpha = 0.2;
        
        self.frame = CGRectMake(self.frame.size.width/2 + self.frame.origin.x, self.frame.origin.y + self.frame.size.height/2, 0, 0);
        
    } completion:^(BOOL finished) {
        [self.item.superContentView removeItem:self.item];
    }];
    
}


@end
